﻿using System;
using System.Transactions;

using YhmOA.IDAL;
using YhmOA.Model;
using YhmOA.Common;

namespace YhmOA.BLL
{
    public static class Integrated_Car_TravelRecordBLL
    {
        private static ITDal<Integrated_Car_TravelRecord> GetTDal()
        {
            return DataProvider.GetGEFDal<ITDal<Integrated_Car_TravelRecord>>();
        }

        /// <summary>
        /// 保存行车记录
        /// </summary>
        /// <param name="isBJ">是否补记</param>
        /// <param name="isCX">是否冲销</param>
        /// <param name="mileage">补记冲销里程</param>
        /// <param name="info">行车记录</param>
        /// <returns>操作是否成功</returns>
        public static bool Save(bool isBJ, bool isCX, decimal mileage, Integrated_Car_TravelRecord info)
        {
            ITDal<Integrated_Car_TravelRecord> dal = GetTDal();
            bool isSucceed = false;
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    //是否补记冲销
                    if (isBJ || isCX)
                    {
                        Integrated_Car_TravelRecord newInfo = new Integrated_Car_TravelRecord();
                        newInfo.SendMileage = mileage;
                        newInfo.ReturnMileage = info.SendMileage;
                        newInfo.CurrentMileage = newInfo.ReturnMileage - newInfo.SendMileage;
                        newInfo.PlateNum = info.PlateNum;
                        newInfo.Flag = isBJ ? 1 : 2;
                        newInfo.SN = isBJ ? "BJ" + DateTime.Now.ToString("yyyyMMddHHmmss") : "CX" + DateTime.Now.ToString("yyyyMMddHHmmss");
                        dal.Add(newInfo);
                    }
                    dal.Add(info);
                    ts.Complete();
                    isSucceed = true;
                }
                catch (Exception ex)
                {
                    isSucceed = false;
                    Log.info(ex.Message);
                }
            }

            return isSucceed;
        }

        /// <summary>
        /// 更新行车记录
        /// </summary>
        /// <param name="isBJ">是否补记</param>
        /// <param name="isCX">是否冲销</param>
        /// <param name="mileage">补记冲销里程</param>
        /// <param name="info">行车记录</param>
        /// <returns>操作是否成功</returns>
        public static bool Update(bool isBJ, bool isCX, decimal mileage, Integrated_Car_TravelRecord info)
        {
            ITDal<Integrated_Car_TravelRecord> dal = GetTDal();
            bool isSucceed = false;
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    //是否补记冲销
                    if (isBJ || isCX)
                    {
                        Integrated_Car_TravelRecord newInfo = new Integrated_Car_TravelRecord();
                        newInfo.SendMileage = mileage;
                        newInfo.ReturnMileage = info.SendMileage;
                        newInfo.CurrentMileage = newInfo.ReturnMileage - newInfo.SendMileage;
                        newInfo.PlateNum = info.PlateNum;
                        newInfo.Flag = isBJ ? 1 : 2;
                        newInfo.SN = isBJ ? "BJ" + DateTime.Now.ToString("yyyyMMddHHmmss") : "CX" + DateTime.Now.ToString("yyyyMMddHHmmss");
                        dal.Add(newInfo);
                    }
                    dal.DeleteByID(info.ID);
                    info.ID = 0;
                    dal.Add(info);
                    ts.Complete();
                    isSucceed = true;
                }
                catch (Exception ex)
                {
                    isSucceed = false;
                    Log.info(ex.Message);
                }
            }

            return isSucceed;
        }

        /// <summary>
        /// 根据车牌号码取得最后一次行车记录
        /// </summary>
        /// <param name="plateNum">车牌号码</param>
        /// <returns>最后一次行车记录</returns>
        public static Integrated_Car_TravelRecord GetLastInfoByPlateNum(string plateNum)
        {
            Integrated_Car_TravelRecord info = GetTDal().GetSingle(string.Format("PlateNum=\"{0}\"", plateNum), "ID DESC");
            if (info == null)
            {
                info = new Integrated_Car_TravelRecord();
            }
            return info;
        }

        /// <summary>
        /// 根据车牌号码和记录ID取得上次行车记录
        /// </summary>
        /// <param name="plateNum">车牌号码</param>
        /// <param name="id">记录ID</param>
        /// <returns>上次行车记录</returns>
        public static Integrated_Car_TravelRecord GetPreInfoByPlateNum(string plateNum, long id)
        {
            Integrated_Car_TravelRecord info = GetTDal().GetSingle(string.Format("PlateNum=\"{0}\" AND ID < {1}", plateNum, id), "ID DESC");
            if (info == null)
            {
                info = new Integrated_Car_TravelRecord();
            }
            return info;
        }
    }
}
